package com.wmd.core.util;

import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.util.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * 类说明
 * 
 * @author yincheng lu
 * @time 2018年12月21日上午11:45:45
 * @Description rsa签名工具类
 */
public class DigitalSignature {
	public static final String SIGN_TYPE_RSA = "RSA";
	public static final String SIGN_TYPE_RSA2 = "RSA2";
	public static final String SIGN_ALGORITHMS = "SHA1WithRSA";
	public static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA";
	public static final int DEFAULT_BUFFER_SIZE = 8192;



	public static String getSha1(String str){
		if(str==null||str.length()==0){
			return null;
		}
		char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
				'a','b','c','d','e','f'};
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
			mdTemp.update(str.getBytes("UTF-8"));

			byte[] md = mdTemp.digest();
			int j = md.length;
			char buf[] = new char[j*2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
				buf[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(buf);
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	/*public static void main(String[] args) throws Exception {
		String content = "study hard and make progress everyday";
		System.out.println("content :" + content);

		
		  KeyPair keyPair = getKeyPair(); PublicKey publicKey = keyPair.getPublic();
		  PrivateKey privateKey = keyPair.getPrivate();
		  System.out.println(Base64.encodeBase64String(privateKey.getEncoded()));
		 System.out.println(Base64.encodeBase64String(publicKey.getEncoded()));
	//MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCjnBTUl+gBQMGQqLiZk/ZwmybRQUs5ahxOe0V8ckOzoh0aYOFfll7q4xQqD0buZIiv9EalGBvpv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rhtI8o+EpIA791h706lK5RW\r\n" + 
				"		fI3RoEYKZbCa1pxIqn0iT386fH1V1/qX8HkCb0cLJJjYHrNSpH/0b5icYgAfYQSm\r\n" + 
				"		qHMFrJWbAgMBAAECggEALOXPjOpy2VU/E7OTiAOHX2DqbxXidwgGeTAiFEuw2Xjq\r\n" + 
				"		QYk72cMUVxYF60Le6mikajEuRRaFmJ+twbfgfPD8FMl/TMI0uJ/VXFi81qqtho23\r\n" + 
				"		Y/kgqUTiUcM9q2hUxMyYumqCNWOXKeCE0DMTEGMGhCUZAa/pUBRuNHXcYwf7Wtju\r\n" + 
				"		kDUxuMx8J8SLYi3C8MDMZEwIy9+LVlYOcCEQXvpUmCtM11h1lGQ3j2UFFTO9QMnB\r\n" + 
				"		EYzFjVGKeVPVUsnf2hHpCspp5AznGF3kEnIMUXjsa32mkixS/lUcZ7IAZ0ZkdvpS\r\n" + 
				"		Etgv7DDwqmw77x4cY3u6ZcvvOfuwgWaz4EXGamTnYQKBgQD3UvM9lN6raq5fE+/D\r\n" + 
				"		HLhlwK8GJidNhi6ukIC+xXbUAV51kAfqgkXxHQQYdaUtRla9okDRqQgUd20Jw+QC\r\n" + 
				"		TQD9Lr880/Xb6QBOgTTInNFzFpysjaFHVXlTLN5k5ryj/YQiiZJkjtv7e2/SZfsr\r\n" + 
				"		AsX97ys5RLuDeMXS7T9HH8rdkwKBgQDZhHt+pYZdpgaSyaBTXt4mOgd7KKMrbn7j\r\n" + 
				"		WQuulXnUrFfh+ZvQgivUPZWSFJKdVpfwFE4UJst/WU+EDNVl8fy34A75StMIYJbX\r\n" + 
				"		sId0Z1s0Iz8yWP5jWFUO9UnBfAfjobOXdXq72DnywPHC1nBnVjFn2Q1uaL8fpJwM\r\n" + 
				"		aK3Uicis2QKBgGn1CqqOVdsgaDXph6WSb1mAdDtxKxNmcidtVwrj9CWdtIcvPoT0\r\n" + 
				"		bNyAbtm/xRUhdlXl5lksTn7eL0Oovc1SiiLt3RZFRKaDKxN2/VMZBA6HWHYAc2Dp\r\n" + 
				"		OIIuzdRymcyz9uEJCUO1hH84Ac8ky7dAh+xjg6QUUzVfD3lvFNX2w+WrAoGBALO9\r\n" + 
				"		mPrXNsft6KKlP/mEfBekfggUVXkPLoUZUODsHadS3rAZvdq3phYU6aKzo0+mfZji\r\n" + 
				"		fn6CbEgIef+JR1GdfBJJcLYpUWcrwZnsApJ60c+sjI1DIrrfBDJ/GHpszmwYc7nl\r\n" + 
				"		Vw0gKQpiy6QP5QRPIjZJEqM4pS41rMlIOtRVIuFJAoGAFUbi1AfnSkIA1WuxUS4b\r\n" + 
				"		F+xpiUE525bIOGs/1RCj7cIHOK7VY0eT5tvhMG+txr3rLxpBeeAb5XuzsQHIOhpk\r\n" + 
				"		87Q+OXGqgyvyba0u65fLHv0XnSFjgFM0MaxT0DQibkfJFySAJRf96hn+3u2NDDCh\r\n" + 
				"		Dk/K2E0u7LDrmYtp3zD3RsA=";
		String pKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0iVOQ9SOF5+edW0QHkJ3\r\n" + 
				"		VQWN/JGP1OW8hV43vBekiFnf8I9Kr7e14iHOOGZHZo9kIi6YArhqdxU3IXzp9y2L\r\n" + 
				"		KrOdYrg/YmjofcDDGzukm2R7n+naO/8wIgj8IsZD4zX2jEEGsJMUUjHs7PJrpBxa\r\n" + 
				"		9igY/TOA5U1OYK6n2u25VSzOjwbsKpC2gN4/w8SeCwhTP6XjMZ4HtULY7WD+47Xj\r\n" + 
				"		oeZDG+RAD46MMZ7B8kOJm6JmGpBYC9oCXvK4bSPKPhKSAO/dYe9OpSuUVnyN0aBG\r\n" + 
				"		CmWwmtacSKp9Ik9/Onx9Vdf6l/B5Am9HCySY2B6zUqR/9G+YnGIAH2EEpqhzBayV\r\n" + 
				"		mwIDAQAB";
		PublicKey publicKey = getPublicKeyFromX509(SIGN_TYPE_RSA, new ByteArrayInputStream(pKey.getBytes()));
		PrivateKey privateKey = getPrivateKeyFromPKCS8(SIGN_TYPE_RSA, new ByteArrayInputStream(rKey.getBytes()));
		System.out.println(Base64.encodeBase64String(privateKey.getEncoded()));
		System.out.println(Base64.encodeBase64String(publicKey.getEncoded()));
		
		String md5Sign = getMd5Sign(content, privateKey);
		System.out.println("sign with md5 and rsa :" + md5Sign);
		boolean md5Verifty = verifyWhenMd5Sign(content, md5Sign, publicKey);
		System.out.println("verify sign with md5 and rsa :" + md5Verifty);

		String sha1Sign = getSha1Sign(content, privateKey);
		System.out.println("sign with sha1 and rsa :" + sha1Sign);
		boolean sha1Verifty = verifyWhenSha1Sign(content, sha1Sign, publicKey);
		System.out.println("verify sign with sha1 and rsa :" + sha1Verifty);
		
		
		System.out.println(testKey(pKey,rKey));
	}*/
	/**
	 * 验证key
	 * author:yincheng lu
	 * Last_update 2018年12月24日上午11:43:40 
	 *	@param publicKey
	 *	@param privateKey
	 *	@return
	 */
	public static int testKey(String publicKey,String privateKey) {
		PublicKey publicKey2;
		PrivateKey privateKey2;
		try {
			 publicKey2 = getPublicKeyFromX509(SIGN_TYPE_RSA, new ByteArrayInputStream(publicKey.getBytes()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;//公钥格式错误
		}
		try {
			 privateKey2 = getPrivateKeyFromPKCS8(SIGN_TYPE_RSA, new ByteArrayInputStream(privateKey.getBytes()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -2;//私钥格式错误
		}
		String content="test rsa keys";
		String md5Sign;
		try {
			md5Sign = getMd5Sign(content, privateKey2);
			System.out.println("sign by key with md5 and rsa :" + md5Sign);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -3;//签名错误
		}
		boolean md5Verifty;
		try {
			md5Verifty = verifyWhenMd5Sign(content, md5Sign, publicKey2);
			System.out.println("key verify with md5 and rsa :" + md5Verifty);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -4;//验签错误
		}
		if(md5Verifty) {
			return 0;
		}else {
			return -5;//验签失败
		}
	}
	/**
	 *  生成密钥对
	 * author:yincheng lu
	 * Last_update 2018年12月24日上午11:43:55 
	 *	@return
	 *	@throws Exception
	 */
	static KeyPair getKeyPair() throws Exception {
		KeyPairGenerator keyGen = KeyPairGenerator.getInstance(SIGN_TYPE_RSA);
		keyGen.initialize(2048); // 可以理解为：加密后的密文长度，实际原文要小些 越大 加密解密越慢
		KeyPair keyPair = keyGen.generateKeyPair();
		return keyPair;
	}

	// 用md5生成内容摘要，再用RSA的私钥加密，进而生成数字签名
	public static String getMd5Sign(String content, PrivateKey privateKey) throws Exception {
		byte[] contentBytes = content.getBytes("utf-8");
		Signature signature = Signature.getInstance("MD5withRSA");
		signature.initSign(privateKey);
		signature.update(contentBytes);
		byte[] signs = signature.sign();
		return Base64.encodeBase64String(signs);
	}

	// 对用md5和RSA私钥生成的数字签名进行验证
	public static boolean verifyWhenMd5Sign(String content, String sign, PublicKey publicKey) throws Exception {
		byte[] contentBytes = content.getBytes("utf-8");
		Signature signature = Signature.getInstance("MD5withRSA");
		signature.initVerify(publicKey);
		signature.update(contentBytes);
		return signature.verify(Base64.decodeBase64(sign));
	}

	// 用sha1生成内容摘要，再用RSA的私钥加密，进而生成数字签名
	public static String getSha1Sign(String content, PrivateKey privateKey) throws Exception {
		byte[] contentBytes = content.getBytes("utf-8");
		Signature signature = Signature.getInstance("SHA1withRSA");
		signature.initSign(privateKey);
		signature.update(contentBytes);
		byte[] signs = signature.sign();
		return Base64.encodeBase64String(signs);
	}

	// 对用md5和RSA私钥生成的数字签名进行验证
	public static boolean verifyWhenSha1Sign(String content, String sign, PublicKey publicKey) throws Exception {
		byte[] contentBytes = content.getBytes("utf-8");
		Signature signature = Signature.getInstance("SHA1withRSA");
		signature.initVerify(publicKey);
		signature.update(contentBytes);
		return signature.verify(Base64.decodeBase64(sign));
	}

	public static PrivateKey getPrivateKeyFromPKCS8(String algorithm, InputStream ins) throws Exception {
		if (ins == null || StringUtils.isEmpty(algorithm)) {
			return null;
		}

		KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
		byte[] encodedKey = readText(ins).getBytes();
		encodedKey = Base64.decodeBase64(encodedKey);
		return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
	}

	public static PublicKey getPublicKeyFromX509(String algorithm, InputStream ins) throws Exception {
		KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
		StringWriter writer = new StringWriter();
		io(new InputStreamReader(ins), writer, -1);
		byte[] encodedKey = writer.toString().getBytes();
		encodedKey = Base64.decodeBase64(encodedKey);
		return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
	}

	private static String readText(InputStream ins) throws IOException {
		Reader reader = new InputStreamReader(ins);
		StringWriter writer = new StringWriter();
		io(reader, writer, -1);
		return writer.toString();
	}

	private static void io(Reader in, Writer out, int bufferSize) throws IOException {
		if (bufferSize == -1) {
			bufferSize = DEFAULT_BUFFER_SIZE >> 1;
		}
		char[] buffer = new char[bufferSize];
		int amount;
		while ((amount = in.read(buffer)) >= 0) {
			out.write(buffer, 0, amount);
		}
	}

	//加密
	public static String encryptAES_CEC_Padding0(String data, String key,String ivString) {
		//偏移量
		byte[] iv = ivString.getBytes();
		try {
			Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
			int blockSize = cipher.getBlockSize();
			byte[] dataBytes = data.getBytes();
			int length = dataBytes.length;
			//计算需填充长度
			if (length % blockSize != 0) {
				length = length + (blockSize - (length % blockSize));
			}
			byte[] plaintext = new byte[length];
			//填充
			System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
			SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
			//设置偏移量参数
			IvParameterSpec ivSpec = new IvParameterSpec(iv);
			cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
			byte[] encryped = cipher.doFinal(plaintext);
			return Base64.encodeBase64String(encryped);

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	//解密
	public static String desEncryptAES_CEC_Padding0(String data, String key,String ivString) {
		byte[] iv = ivString.getBytes();

		try {
			byte[] encryp = Base64.decodeBase64(data);
			Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
			SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
			IvParameterSpec ivSpec = new IvParameterSpec(iv);
			cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
			byte[] original = cipher.doFinal(encryp);
			return new String(original).trim();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args){
		String key="pzqpbhys8khxp0arfchflupkpmdjo7u7";
		String lv="1234567890abcdef";
		String data="a";
		String r=encryptAES_CEC_Padding0(data,key,lv);
		System.out.println(r);
		String data2="1";
		String r2=encryptAES_CEC_Padding0(data2,key,lv);
		System.out.println(r2);
		String r3=desEncryptAES_CEC_Padding0("lKxe4PUMhUtkTamd9M2Z0EkRmZkKYxlDenwF4RUgDPvkTJUFZ+jttJz6MEQlDehSzP8X4qa7ykoAENeu76J4bjFr5ohUTo25708iQ7ep/Us=",key,lv);
		System.out.println(r3);
	}



}
